﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sy1_7
{
    public partial class Form1 : Form
    {
        private Size currentSize = new Size(240, 180);
        private readonly Size maxSize = new Size(240, 180);
        private readonly Size minSize = new Size(24, 18);
        private string imagePath = "butterfly.jpg"; 

        public Form1()
        {
            InitializeComponent();

            this.SetStyle(ControlStyles.OptimizedDoubleBuffer |
                         ControlStyles.AllPaintingInWmPaint |
                         ControlStyles.UserPaint, true);

            this.DoubleBuffered = true;
            this.Size = new Size(350, 350);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            LoadProgram();
        }

        private void LoadProgram()
        {
            SetupButtons();

            SetupPictureBox();

            UpdateButtonStates();
        }

        private void SetupButtons()
        {

            btnZoomIn.Text = "放大";
            btnZoomOut.Text = "缩小";
            btnZoomIn.Size = new Size(80, 30);
            btnZoomOut.Size = new Size(80, 30);
            btnZoomIn.Location = new Point(80, 250);
            btnZoomOut.Location = new Point(180, 250);
            btnZoomIn.Click += new EventHandler(SafeZoomInClick);
            btnZoomOut.Click += new EventHandler(SafeZoomOutClick);

            btnZoomIn.BringToFront();
            btnZoomOut.BringToFront();
        }

        private void SetupPictureBox()
        {
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox1.Size = currentSize;
            pictureBox1.Location = new Point(55, 50);
            pictureBox1.BorderStyle = BorderStyle.FixedSingle;

            LoadImageFromDebug();
        }

        private void LoadImageFromDebug()
        {
            try
            {
                string currentDir = Directory.GetCurrentDirectory();
                string fullPath = Path.Combine(currentDir, imagePath);

                Console.WriteLine($"尝试从路径加载图片: {fullPath}");

                if (File.Exists(fullPath))
                {

                    using (FileStream fs = new FileStream(fullPath, FileMode.Open, FileAccess.Read))
                    {
                        Image originalImage = Image.FromStream(fs);
                        pictureBox1.Image = new Bitmap(originalImage);
                        originalImage.Dispose();
                    }
                    MessageBox.Show($"图片加载成功！\n路径: {fullPath}", "成功");
                    return;
                }

                string[] imageExtensions = { "*.jpg", "*.jpeg", "*.png", "*.bmp", "*.gif" };
                string foundImagePath = FindImageInDirectory(currentDir, imageExtensions);

                if (!string.IsNullOrEmpty(foundImagePath))
                {
                    using (FileStream fs = new FileStream(foundImagePath, FileMode.Open, FileAccess.Read))
                    {
                        Image originalImage = Image.FromStream(fs);
                        pictureBox1.Image = new Bitmap(originalImage);
                        originalImage.Dispose();
                    }
                    MessageBox.Show($"找到并加载图片: {Path.GetFileName(foundImagePath)}", "成功");
                    return;
                }

                CreateTestImage();
                MessageBox.Show($"未找到图片文件，已创建测试图片。\n请将图片放入目录: {currentDir}", "提示");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"图片加载失败: {ex.Message}\n将使用测试图片。", "错误");
                CreateTestImage();
            }
        }

        private string FindImageInDirectory(string directory, string[] extensions)
        {
            foreach (string extension in extensions)
            {
                try
                {
                    string[] files = Directory.GetFiles(directory, extension);
                    if (files.Length > 0)
                    {
                        return files[0]; 
                    }
                }
                catch
                {
                }
            }
            return null;
        }

        private void CreateTestImage()
        {
           

            if (pictureBox1.Image != null)
            {
                var oldImage = pictureBox1.Image;
                pictureBox1.Image = null;
                oldImage.Dispose();
            }
        }

        private void SafeZoomInClick(object sender, EventArgs e)
        {
            try
            {
                btnZoomIn.Enabled = false;
                btnZoomOut.Enabled = false;

                ZoomIn();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"放大操作错误: {ex.Message}");
            }
            finally
            {
                UpdateButtonStates();
            }
        }

        private void SafeZoomOutClick(object sender, EventArgs e)
        {
            try
            {
                btnZoomIn.Enabled = false;
                btnZoomOut.Enabled = false;

                ZoomOut();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"缩小操作错误: {ex.Message}");
            }
            finally
            {
                UpdateButtonStates();
            }
        }

        private void ZoomIn()
        {
            int newWidth = currentSize.Width + 24;
            int newHeight = currentSize.Height + 18;

            if (newWidth > maxSize.Width) newWidth = maxSize.Width;
            if (newHeight > maxSize.Height) newHeight = maxSize.Height;

            currentSize = new Size(newWidth, newHeight);
            pictureBox1.Size = currentSize;
        }

        private void ZoomOut()
        {
            int newWidth = currentSize.Width - 24;
            int newHeight = currentSize.Height - 18;

            if (newWidth < minSize.Width) newWidth = minSize.Width;
            if (newHeight < minSize.Height) newHeight = minSize.Height;

            currentSize = new Size(newWidth, newHeight);
            pictureBox1.Size = currentSize;
        }

        private void UpdateButtonStates()
        {
            btnZoomIn.Enabled = (currentSize.Width < maxSize.Width);
            btnZoomOut.Enabled = (currentSize.Width > minSize.Width);

            this.Text = $"图片缩放 - 尺寸: {currentSize.Width}×{currentSize.Height}";
        }
        private void pictureBox1_DoubleClick(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "图片文件|*.jpg;*.jpeg;*.png;*.bmp;*.gif|所有文件|*.*";
            openFileDialog.Title = "选择图片文件";

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    string selectedFile = openFileDialog.FileName;
                    string destFile = Path.Combine(Directory.GetCurrentDirectory(), Path.GetFileName(selectedFile));
                    File.Copy(selectedFile, destFile, true);
                    imagePath = Path.GetFileName(selectedFile);
                    LoadImageFromDebug();
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"图片加载失败: {ex.Message}", "错误");
                }
            }
        }
    }
}
